//
//  MineViewController.m
//  StandardizedProduct
//
//  Created by 宁宁 on 16/6/21.
//  Copyright © 2016年 ym. All rights reserved.
//

#import "MineViewController.h"

#import "MineHeaderView.h"//页眉View

//TableViewCell
#import "MyOrderTableViewCell.h"//我的订单Cell
#import "GoodsStatesTableViewCell.h"//订单状态Cell
#import "MyCollectionTableViewCell.h"//收藏Cell
#import "MyElseTableViewCell.h"//其他Cell
//CollectionViewCell
#import "GoodsStatesCollectionViewCell.h"//订单状态Cell

#import "LoginViewController.h"//登录Controller
#import "RegisterViewController.h"//注册Controller
#import "MyOrderViewController.h"//我的订单Controller


#import "NetWorkManager.h"//网络请求
#import "UserInformationModel.h"//用户信息Model

#define GoodsStatesCellHeight 80
#define CollectionCellHeight 120

@interface MineViewController ()<UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UITableView *mineTableView;

@end

@implementation MineViewController
{
    MineHeaderView *_headerView;
    
    UICollectionView *_goodsStatesCollectionView;//订单CollectionView
    NSArray *_goodsStatesArray;
    UICollectionView *_collectionCollectionView;//收藏CollectionView
    NSArray *_collectionArray;
    
    
    NSUserDefaults *defaults;
}

 static NSString *const MineHeaderViewID = @"MineHeaderView";
 static NSString *const MyOrderTableViewCellID = @"MyOrderTableViewCell";
 static NSString *const GoodsStatesTableViewCellID = @"GoodsStatesTableViewCell";
 static NSString *const GoodsStatesCollectionViewCellID = @"GoodsStatesCollectionViewCell";
 static NSString *const MyCollectionTableViewCellID = @"MyCollectionTableViewCell";
 static NSString *const MyElseTableViewCellID = @"MyElseTableViewCell";

- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = NO;
    
    if ([defaults objectForKey:@"UserName"]) {
        //网络请求
//        [self network];
    }
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    defaults = [NSUserDefaults standardUserDefaults];
    
    //设置导航栏
    [self setNav];
    
    //初始化数据源
    [self createData];
    
    //注册TableView
    [self settingTableView];
}

#pragma mark === 设置导航栏 ===
- (void)setNav{
    self.title = @"会员中心";
    UIBarButtonItem *back = [[UIBarButtonItem alloc] init];
    back.title = @"返回";
    self.navigationItem.backBarButtonItem = back;
}

#pragma mark === 初始化数据源 ===
- (void)createData{
    _goodsStatesArray = @[@"待付款", @"待发货", @"待收货", @"待评价"];
    _collectionArray = @[@"收藏的品牌", @"收藏的单品", @"浏览记录"];
}

#pragma mark === 注册TableView ===
- (void)settingTableView{
    //页眉点击事件
    _headerView = [[[NSBundle mainBundle] loadNibNamed:@"MineHeaderView" owner:nil options:nil] lastObject];
    self.mineTableView.tableHeaderView = _headerView;
    [_headerView.setting addTarget:self action:@selector(jumpToSetting) forControlEvents:UIControlEventTouchUpInside];
    [_headerView.message addTarget:self action:@selector(jumpToMessage) forControlEvents:UIControlEventTouchUpInside];
    [_headerView.icon addTarget:self action:@selector(jumpToChangeIcon) forControlEvents:UIControlEventTouchUpInside];
    [_headerView.login addTarget:self action:@selector(jumpToLogin) forControlEvents:UIControlEventTouchUpInside];
    [_headerView.registerr addTarget:self action:@selector(jumpToRegister) forControlEvents:UIControlEventTouchUpInside];
    
    //注册页眉
    [self.mineTableView registerNib:[UINib nibWithNibName:@"MineHeaderView" bundle:nil] forCellReuseIdentifier:MineHeaderViewID];
    
    //注册自定义cell
    [self.mineTableView registerNib:[UINib nibWithNibName:@"MyOrderTableViewCell" bundle:nil] forCellReuseIdentifier:MyOrderTableViewCellID];
    [self.mineTableView registerClass:[GoodsStatesTableViewCell class] forCellReuseIdentifier:GoodsStatesTableViewCellID];
    [self.mineTableView registerClass:[MyCollectionTableViewCell class] forCellReuseIdentifier:MyCollectionTableViewCellID];
    [self.mineTableView registerNib:[UINib nibWithNibName:@"MyElseTableViewCell" bundle:nil] forCellReuseIdentifier:MyElseTableViewCellID];
}




#pragma mark === TableViewDelegate ===
//section数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return 2;
        case 1:
            return 1;
        case 2:
            return 2;
        case 3:
            return 1;
        default:
            return 0;
    }
}
//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:
            if (indexPath.row == 0) {
                return 45;
            }else return GoodsStatesCellHeight;
        case 1:
            return CollectionCellHeight;
        case 2:
            return 70;
        case 3:
            return 70;
        default:
            return 0;
    }
}
//ReusableCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        //我的订单
        if (indexPath.row == 0) {
            MyOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyOrderTableViewCellID];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        //待付款 待发货 待收货 待评价
        }else{
            GoodsStatesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:GoodsStatesTableViewCellID];
            _goodsStatesCollectionView = cell.collectionView;
            _goodsStatesCollectionView.delegate = self;
            _goodsStatesCollectionView.dataSource = self;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
    }
    
    //收藏的品牌 收藏的单品 浏览记录
    else if (indexPath.section == 1){
        MyCollectionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyCollectionTableViewCellID];
        _collectionCollectionView = cell.collectionView;
        _collectionCollectionView.delegate = self;
        _collectionCollectionView.dataSource = self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    //地址管理 我的优惠券 意见反馈
    MyElseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyElseTableViewCellID];
    if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            cell.title.text = @"地址管理";
            cell.imageview.image = [UIImage imageNamed:@"地址管理"];
        }else {
            cell.title.text = @"我的优惠券";
            cell.imageview.image = [UIImage imageNamed:@"我的优惠券"];
        }
    }else {
        cell.title.text = @"意见反馈";
        cell.imageview.image = [UIImage imageNamed:@"意见反馈"];
    }
    [cell setSeparatorInset:UIEdgeInsetsZero];
    [cell setLayoutMargins:UIEdgeInsetsZero];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
//SelectRow
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:
            //跳转到我的订单
            if (indexPath.row == 0) {
                [self jumpToMyOrder:0];
            }
            break;
        case 2:
            //跳转到地址管理
            if (indexPath.row == 0) {
                NSLog(@"地址管理");
            //跳转到我的优惠券
            }else  NSLog(@"我的优惠券");
            break;
        case 3:
            //跳转到意见反馈
            NSLog(@"意见反馈");
            if ([defaults objectForKey:@"Id"]) {
                [defaults removeObjectForKey:@"Id"];
                [self setHeaderViewButton:NO];
            }
            break;
        default:
            break;
    }
}




#pragma mark === collectionViewDelegate ===
//item数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    //待付款 待发货 待收货 待评价
    if (collectionView == _goodsStatesCollectionView) {
        return 4;
    //收藏的品牌 收藏的单品 浏览记录
    }else return 3;
}
//item大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    //待付款 待发货 待收货 待评价
    if (collectionView == _goodsStatesCollectionView) {
        return CGSizeMake(WIDTH / 4, GoodsStatesCellHeight);
    //收藏的品牌 收藏的单品 浏览记录
    }else return CGSizeMake((WIDTH - 2) / 3, CollectionCellHeight);
    
}
//四边边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
//ReusableCell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    GoodsStatesCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:GoodsStatesCollectionViewCellID forIndexPath:indexPath];
    //待付款 待发货 待收货 待评价
    if (collectionView == _goodsStatesCollectionView) {
        cell.title.text = _goodsStatesArray[indexPath.item];
        cell.imageView.image = [UIImage imageNamed: [NSString stringWithFormat:@"order%ld", indexPath.item]];
    //收藏的品牌 收藏的单品 浏览记录
    }else{
        cell.top.constant = 20;
        cell.title.text = _collectionArray[indexPath.item];
        cell.title.font = [UIFont systemFontOfSize:16];
        cell.title.textColor = [UIColor blackColor];
        cell.imageView.image = [UIImage imageNamed: [NSString stringWithFormat:@"collection%ld", indexPath.item]];
    }
    return cell;
}
//SelectItem
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    //待付款 待发货 待收货 待评价
    if (collectionView == _goodsStatesCollectionView) {
        [self jumpToMyOrder:indexPath.item + 1];
    //收藏的品牌 收藏的单品 浏览记录
    }else NSLog(@"%@", _collectionArray[indexPath.item]);
}




#pragma mark === 跳转到设置界面 ===
- (void)jumpToSetting{
    NSLog(@"设置");
}
#pragma mark === 跳转到消息界面 ===
- (void)jumpToMessage{
    NSLog(@"消息");
}
#pragma mark === 跳转到更换头像界面 ===
- (void)jumpToChangeIcon{
    NSLog(@"更换头像");
    [self changeInfo];
}
#pragma mark === 跳转到登录界面 ===
- (void)jumpToLogin{
    LoginViewController *login = [[LoginViewController alloc] init];
    login.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:login animated:YES];
    self.navigationController.navigationBar.hidden = YES;
}
#pragma mark === 跳转到注册界面 === 
- (void)jumpToRegister{
    RegisterViewController *regi = [[RegisterViewController alloc] init];
    regi.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:regi animated:YES];
    self.navigationController.navigationBar.hidden = YES;
}
#pragma mark === 跳转到我的订单 ===
- (void)jumpToMyOrder:(NSInteger)index{
    MyOrderViewController *order = [[MyOrderViewController alloc] init];
    order.index = index;
    order.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:order animated:YES];
}


//隐藏登录注册按钮
- (void)setHeaderViewButton:(BOOL)bo{
    _headerView.login.hidden = bo;
    _headerView.loginLabel.hidden = bo;
    _headerView.registerr.hidden = bo;
    _headerView.registerLabel.hidden = bo;
    
    if (bo == NO) {
        _headerView.userName.text = @"未登录";
    }
    
}
//网络请求
- (void)network{
    
    [self setHeaderViewButton:YES];
    
    NSString *Id = [defaults objectForKey:@"Id"];
    
    NSDictionary *paraments = @{
                                @"Id" : Id
                                };
    
    UserInformationModel *model = [[UserInformationModel alloc] init];
    
    [NetWorkManager requestWithType:HttpRequestTypeGet withMethodName:@"Member/GetById" withParaments:paraments withSuccessBlock:^(NSDictionary *object) {
        NSLog(@"%@", object);
        
        //将信息存入model
        [model setValuesForKeysWithDictionary:object[@"Rows"]];
        
        //显示用户名
        if ([object[@"Rows"][@"UserName"] isEqualToString:@""]) {
            _headerView.userName.text = @"已登录";
        }else _headerView.userName.text = model.UserName;
        
        
    } withFailureBlock:^(NSError *error) {
        NSLog(@"获取用户信息失败: %@", error);
    } progress:nil];
    
}

- (void)changeInfo{
    
}

- (UITableView *)mineTableView{
    if (!_mineTableView) {
        _mineTableView= [[UITableView alloc]initWithFrame:CGRectMake(0,0, WIDTH, HEIGHT - 44) style:UITableViewStyleGrouped];
        _mineTableView.sectionFooterHeight = 10.0;
        _mineTableView.sectionHeaderHeight = 0;
        _mineTableView.delegate = self;
        _mineTableView.dataSource = self;
        _mineTableView.showsVerticalScrollIndicator = NO;//去掉滑动竖条
        [self.view addSubview:_mineTableView];
    }
    return _mineTableView;
}
@end
